import Vue from 'vue'
import VueRouter from 'vue-router'
const Login=()=>import('../components/Login')
const Home=()=>import('../components/Home')
const Welcome=()=>import('../components/Welcome')

Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    redirect:'/login'
  },
  {
    path:'/login',
    component:Login
  },
  {
    path:'/home',
    component:Home,
    redirect:'/welcome',
    children:[
      {path:'/welcome',component:Welcome}
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
});

//挂载路由导航守卫
router.beforeEach((to,from,next)=>{
   if (to.path==='/login') return next();
   const tokenStr = window.sessionStorage.getItem('token')
       if (tokenStr) return  next();
         next('/login')

})
export default router
